<?php

/**
 * vtnsCart provides basic Cart utility methods.
 *
 * @package    vtns
 * @subpackage util
 * @author     Ky Anh Truong <kyanh.truong@vtns.vn>
 * @version    SVN: $Id: vtnsCart.class.php 29525 2012-08-28 15:00:00Z kyanh $
 */
 
class vtnsCart
{
	public static function display($cart)
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		if ($cart) {
			$totalPrice = 0;
			$totalDiscount = 0;
			if (isset($cart->combos) && count($cart->combos) > 0)
			{
				echo "<div class='cart_item_title'>Combo</div>";
				echo "<input type='hidden' id='cart_combo_to_update' value='".json_encode($cart->combos->getRawValue())."' />";
				echo "<ul>";
				foreach ($cart->combos as $key=>$combo)
				{
					$deal_obj = Doctrine_Core::getTable("VTNSMenuDeal")->find($combo['menu_deal_id']);
					$deal_pizza = Doctrine_Core::getTable("VTNSPizza")->find($combo['pizza_id']);
					$totalPrice += ($deal_obj->price);
					?>
                    <li class="cart_combo">
                    	<div class='cart_item_name'><label for='deal_<?php echo $key; ?>'>&raquo; <?php echo $deal_obj->name; ?></label></div>
                        <div class='cart_item_quantity_wrapper'><input type='number' class='cart_input' id='deal_<?php echo $key; ?>' value='<?php echo $combo['quantity']; ?>' width='50' /></div>
                        <div class="clearfix"></div>
                        <ul>
                        	<li>&raquo; <b><?php echo $deal_pizza->name ?> </li>
                            <?php
							foreach ($combo['attribute_list'] as $deal_attr)
							{
								$deal_attr_obj = Doctrine_Core::getTable("VTNSAttribute")->find($deal_attr);
							?>
                        	<li>&raquo; <b><?php echo VTNSAttribute::$attribute_names[$deal_attr_obj->name] ?></b> : <?php echo $deal_attr_obj->value ?> </li>
                            <?php
							}
							?>
                            <li class="yellow"><b>&raquo; Price:</b> <?php echo format_number((int)$deal_obj->price,"vi_VN") ?> VNĐ</li>
                        </ul>
                    </li>
                    <?
				} 
				echo "</ul>";
			}
			if (isset($cart->pizzas) && count($cart->pizzas) > 0)
			{
				echo "<div class='cart_item_title'>Pizza</div>";
				echo "<input type='hidden' id='cart_pizza_to_update' value='".json_encode($cart->pizzas->getRawValue())."' />";
				echo "<ul>";
				foreach ($cart->pizzas as $key=>$pizza)
				{
					$pizza_obj = Doctrine_Core::getTable("VTNSPizza")->find($pizza['pizza_id']);
					$totalPrice += ((int)$pizza['pizza_price'] * $pizza['pizza_quantity']);
					$totalDiscount += ((int)$pizza['base_price'] - (int)$pizza['pizza_price']) * $pizza['pizza_quantity'];
				?>
                	<li class='cart_pizza'>
                    	<div class='cart_item_name'><label for='pizza_<?php echo $key; ?>'>&raquo; <?php echo $pizza_obj->name; ?></label></div>
                        <div class='cart_item_quantity_wrapper'><input type='number' class='cart_input' id='pizza_<?php echo $key; ?>' value='<?php echo $pizza['pizza_quantity']; ?>' width='50' /></div>
                        <div class="clearfix"></div>
                        <ul>
                        	<?php 
							if ($pizza['pizza_attributes'] && count($pizza['pizza_attributes']) > 0)
							{
								foreach ($pizza['pizza_attributes'] as $attr)
								{
									$attr_obj = Doctrine_Core::getTable("VTNSAttribute")->find($attr);
									?>
									<li>&raquo; <b><?php echo VTNSAttribute::$attribute_names[$attr_obj->name] ?></b> : <?php echo $attr_obj->value ?> </li>
									<?php
								}
							}
							?>
							<li class="yellow"><b>&raquo; Price:</b> <?php echo format_number((int)$pizza['base_price'], "vi_VN")?> VNĐ</li>
                            <?
							if ($pizza['pizza_options'] && count($pizza['pizza_options']) > 0)
							{
								foreach ($pizza['pizza_options'] as $option)
								{
									$option_obj = Doctrine_Core::getTable("VTNSOption")->find($option);
									$totalPrice += ($option_obj->adding_price * $pizza['pizza_quantity']);
									?>
									<li>&raquo; <?php echo $option_obj->name; ?> - <b class="yellow"><?php echo format_number((int)$option_obj->adding_price, "vi_VN")?> VNĐ</b></li>
									<?php
								}
							}
							?>
                        </ul>
                    </li>
                <?php
				}
				echo "</ul>";
			}
			
			if (isset($cart->accompaniments) && count($cart->accompaniments) > 0)
			{
				echo "<div class='cart_item_title'>Side Orders</div>";
				echo "<ul>";
				foreach ($cart->accompaniments as $accompaniment) {
					$accom_obj = Doctrine_Core::getTable("VTNSAccompaniment")->find($accompaniment->accompaniment_id);
					$totalPrice += ($accom_obj->price * $accompaniment->accompaniment_quantity);
					echo "<li class='cart_accom'>";
					echo "<div class='cart_item_name'><label for='accom_".$accom_obj->accompaniment_id."'>&raquo; ".$accom_obj->name."</label></div>";
					echo "<div class='cart_item_quantity_wrapper'><input type='number' class='cart_input' id='accom_".$accom_obj->accompaniment_id."' value='".$accompaniment->accompaniment_quantity."' width='50' /></div>";
					echo "<div class='clearfix'></div>";
					echo "<ul>";
						echo "<li class='yellow'><b>&raquo; Price:</b> ".format_number((int)$accom_obj->price, "vi_VN")." VNĐ</li>";
					echo "</ul>";
					echo "</li>";
				}
				echo "</ul>";
			}
			
			if ($totalPrice) {
				$basePrice = $totalPrice + $totalDiscount;
				echo "
				<div class='vtns_cart_price'>Price: <span class='yellow'>".format_number((int)$basePrice, "vi_VN")." VNĐ</span></div>
				<div class='vtns_cart_price'>Discount: <span class='yellow'>".format_number((int)$totalDiscount, "vi_VN")." VNĐ</span></div>
				<div id='cart_total_price'>Total: ".format_number((int)$totalPrice, "vi_VN")." VNĐ</div><input type='hidden' id='cart_total_price_hide' value='".((int)$totalPrice)."' /><div id='cart_button'><a href='".url_for("@order_summary")."' class='btn btn-success'>Order</a></div><div id='cart_button_update' class='btn'>Update</div><div class='clearfix'></div>";
			}
		}
	}
}
